package org.micode.demo.web.controller;

import org.micode.common.helper.I18nMsgHelper;
import org.micode.demo.web.base.BaseWebAdminController;
import org.micode.common.annotation.NoNeedLogin;
import org.micode.common.domain.ApiResult;
import org.micode.demo.db.cache.user.ISysUserCacheHandler;
import org.micode.demo.db.cache.user.SysUserCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/web-admin/index")
public class IndexController extends BaseWebAdminController {

    @Autowired
    private ISysUserCacheHandler sysUserCacheHandler;

    /**
     * 检查token，提供微信小程序申请验证使用
     *
     * @return
     */
    @NoNeedLogin
    @PostMapping("/checkTokenExpire")
    public ApiResult<Boolean> checkTokenExpire() {
        log.debug(I18nMsgHelper.get("api.call.index.checkTokenExpire"));
        String token = getToken();

        if (StringUtils.isAnyEmpty(token)) {
            log.error(I18nMsgHelper.get("valid.token.not-null"));
            return failResultForInvalidParam(I18nMsgHelper.get("valid.token.not-null"));
        }

        SysUserCache user = sysUserCacheHandler.getByKey(token);
        if (null != user && user.resetExpireTime()) {
            // 已经重设过期时间
            sysUserCacheHandler.setByKey(token, user);
        }

        return successResultWithData(null != user);
    }

}

